package com.lgb.Service;

import com.lgb.DAO.PicturesRepository;
import com.lgb.pojo.Pictures;
import com.lgb.utils.FileUtils;
import com.lgb.vo.PicturesQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class PicturesServicesImpl implements PicturesService{

    @Autowired
    private PicturesRepository picturesRepository;

    @Override
    public Pictures savePictures(Pictures pictures) {
        return picturesRepository.save(pictures);
    }

    @Override
    public List<Pictures> getAllPictures() {
        return picturesRepository.findAll();
    }

    @Override
    public List<PicturesQuery> getAllPicturesQuery(){
        //获取全部数据
        List<Pictures> pictures = picturesRepository.findAll();
        //创建容器
        List<PicturesQuery> picturesQueries = new ArrayList<>();
        //遍历
        for(Pictures picture : pictures){
            PicturesQuery picturesQuery = new PicturesQuery();
            //数据迁移
            picturesQuery.setId(picture.getId());
            picturesQuery.setDescription(picture.getDescription());
            picturesQuery.setName(picture.getName());
            picturesQuery.setUpdateTime(picture.getUpdateTime());
            //获取全部的文件路径
            String Dir_Address = picture.getAddress();
            File folder = new File(Dir_Address);
            File[] files = folder.listFiles();
            //存放所有的文件地址
            String[] filePaths = new String[files.length];
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    filePaths[i] = FileUtils.generateRelativePath(files[i].getAbsolutePath() , "C:\\Users\\86137\\Desktop\\blog\\src\\main\\resources\\static");
                }
            }
            picturesQuery.setAddress(filePaths);
            picturesQueries.add(picturesQuery);
        }
        return picturesQueries;
    }

    @Override
    public PicturesQuery getPictureQuery(Long id) {
        //获取picture对象
        Pictures picture = picturesRepository.getOne(id);
        //数据迁移
        PicturesQuery picturesQuery = new PicturesQuery();
        picturesQuery.setId(picture.getId());
        picturesQuery.setDescription(picture.getDescription());
        picturesQuery.setName(picture.getName());
        picturesQuery.setUpdateTime(picture.getUpdateTime());
        //获取全部的文件路径
        String Dir_Address = picture.getAddress();
        File folder = new File(Dir_Address);
        File[] files = folder.listFiles();
        //存放所有的文件地址
        String[] filePaths = new String[files.length];
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                filePaths[i] = '\\' + FileUtils.generateRelativePath(files[i].getAbsolutePath() , "C:\\Users\\86137\\Desktop\\blog\\src\\main\\resources\\static");
            }
        }
        picturesQuery.setAddress(filePaths);
        return picturesQuery;
    }

    @Override
    public void deletePictures(Long id) {
        //文件删除(必须先文件删除，否则数据库删除后无法找的对应对象)
        Pictures pictures = picturesRepository.getOne(id);
        String address = pictures.getAddress();
        FileUtils.deleteFolder(address);
        //数据库删除
        picturesRepository.deleteById(id);
    }
}
